package System_code;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-08-30
 * Time: 17:45
 * 编写代码, 基于 AtomicInteger 实现多线程自增同一个变量
 */
public class AtomicintegerDemo {
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 5; j++) {
                        System.out.println(count.incrementAndGet());

                    }
                }
            });
            threads[i].start();
        }

    }

}
